package dao.impl;

import bean.Users;
import dao.UsersDao;
import util.Druid;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersDaoImpl extends Druid implements UsersDao {
    //获取链接
    private Connection connection = connection = getConnection();;
    private ResultSet resultSet = null;
    private PreparedStatement pps = null;
    //查询所有
    @Override
    public List<Users> getAllUsers() {
        //建立集合，存储users对象
        List<Users> list = new ArrayList<Users>();
        //创建与状态通道
        try {
            String sql = "SELECT * FROM users";
            pps = connection.prepareStatement(sql);
            resultSet = pps.executeQuery();
            while(resultSet.next()) {
                Users user = new Users();
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("password"));
                list.add(user);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //释放资源
            close(connection,resultSet,pps);
        }
        return list;
    }
    //自动填写通过ID
    @Override
    public Users getUser(Integer id){
        Users user = new Users();
        String sql = "SELECT * from users where userId = ?";
        try {
            pps = connection.prepareStatement(sql);
            pps.setInt(1,id);
            resultSet = pps.executeQuery();
            if(resultSet.next()) {
                user.setUsername(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("passWord"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,resultSet,pps);
        }
        return user;
    }
    //注册新用户
    @Override
    public boolean updateUser(String username,String password) {
        String sql = "INSERT INTO users(userName,passWord) VALUES(?,?)";
        try {
            pps = connection.prepareStatement(sql);
            pps.setString(1,username);
            pps.setString(2,password);
            int i = pps.executeUpdate();
            if (i > 0) {
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,resultSet,pps);
        }
        return false;
    }
    //登录验证
    @Override
    public Users login(String username,String password) {
        String sql = "SELECT * from users where userName = ? and passWord = ?";
        try {
            pps = connection.prepareStatement(sql);
            pps.setString(1,username);
            pps.setString(2,password);
            resultSet = pps.executeQuery();
            //数据与数据库中匹配，返回该对象，否则返回null
            if (resultSet.next()) {
                Users user = new Users();
                user.setUsername(resultSet.getString("userName"));
                user.setPassword(resultSet.getString("passWord"));
                return user;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,resultSet,pps);
        }
        return null;


    }

    @Override
    public boolean getUser(String username) {
        //通过用户名查找
        String sql = "SELECT * from users where userName = ?";
        try {
            pps = connection.prepareStatement(sql);
            pps.setString(1,username);
            resultSet = pps.executeQuery();
            if (resultSet.next()) {
                //用户名存在，返回true
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(connection,resultSet,pps);
        }

        //不存在，返回false
        return false;
    }
}
